﻿using System;
using BotonJXC.DAO;
using BotonJXC.Util;
using BotonJXC.Entity;
using NHibernate;

namespace BotonJXC.BLL
{
    /// <summary>
    /// 单据编号产生器
    /// </summary>
    public class BillSNGenerator
    {
        StoreBLL _storeBll;

        /// <summary>
        /// 产生单据编号
        /// </summary>
        /// <param name="billType">单据类型</param>
        /// <param name="billSource">单据源</param>
        /// <param name="store">门店</param>
        /// <returns></returns>
        public string GeneratorBillSn(BillType billType, BillSource billSource, Store store)
        {
            Type type = getEntityType(billType);
            if (type == null)
                return null;
            string regionSn = null;
            if (billSource == BillSource.Store)
            {
                regionSn = store.Region.FDBillPrefix;
            }
            else
            {
                regionSn = store.Region.OTBillPrefix;
            }
            string timeField = GetTimeField(billType);
            string hql = "select count(*) from " + type.Name + " where " + timeField + ">=? and "+timeField+"<=?";
            IQuery query = HBHelper.Session.CreateQuery(hql);
            query.SetParameter(0, DateTime.Now.Date);
            query.SetParameter(1, new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59));
            long count = ((long)query.UniqueResult()) + 1;
            return regionSn + store.StoreSn + DateTime.Now.ToString("yyyyMMdd") + count.ToString().PadLeft(4, '0');
        }

        private string GetTimeField(BillType billType)
        {
            //if (billType == BillType.InStock)
            //{
            //    return "";
            //}
            //else if (billType == BillType.Order)
            //{
            //    return "";
            //}
            //else if (billType == BillType.OutStock)
            //{
            //    return "";
            //}
            //else if (billType == BillType.Allocate)
            //{
            //    return "";
            //}
            return "CreateTime";
        }

        public string GeneratorBillSn(BillType billType, BillSource billSource, int storeId)
        {
            return GeneratorBillSn(billType, billSource, GetStoreBll().Get(storeId));
        }

        private StoreBLL GetStoreBll()
        {
            if (_storeBll == null)
                _storeBll = new StoreBLL();
            return _storeBll;
        }
        private Type getEntityType(BillType billType)
        {
            if (billType == BillType.InStock)
            {
                return typeof(InStocks);
            }
            else if (billType == BillType.Order)
            {
                return typeof(Orders);
            }
            else if (billType == BillType.OutStock)
            {
                return typeof(OutStocks);
            }
            else if (billType == BillType.Allocate)
            {
                return typeof(Allocates);
            }
            else if (billType == BillType.Return)
            {
                return typeof(ReturnedBlls);
            }
            else if (billType == BillType.Pickings)
            {
                return typeof(Pickings);
            }
            else if (billType == BillType.Acceptance)
            {
                return typeof(Acceptance);
            }
            else if (billType == BillType.FinanceBill)
            {
                return typeof(FinanceBills);
            }
            return null;
        }
    }
}
